package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fengpanlong.entity.Gogal;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class GogalDao {
	private DBHelp<Gogal> db = new DBHelp<Gogal>();
	
	private class GogalRowMapper implements RowMapper<Gogal> {

		public Gogal mapperRow(ResultSet rs) throws SQLException {
			Gogal g = new Gogal();
			g.setId(rs.getString("id"));
			g.setName(rs.getString("name"));
			g.setDesc(rs.getString("desc"));
			g.setCreatetime(rs.getString("createtime"));
			g.setEmployeeId(rs.getString("employeeId"));
			g.setProjectId(rs.getString("projectId"));
			return g;
		}
		
	}
	/**
	 * 通过项目id查找目标
	 * @param projectId
	 * @return
	 */
	public List<Gogal> findGogalsByProjectId(String projectId) {
		String sql = "select id,name,`desc`,createtime,employeeid,projectid from t_gogal where projectid = ? order by createtime desc";
		return db.executeQueryForList(sql, new GogalRowMapper(), projectId);
	}
	/**
	 * 通过目标id查找目标
	 * @param gogalId 目标id
	 * @return 目标
	 */
	public Gogal findById(String gogalId) {
		String sql = "select id,name,`desc`,createtime ,employeeid,projectid from t_gogal where id = ?";
		return db.executeQueryForObject(sql, new GogalRowMapper(), gogalId);
	}
	/**
	 * 保存目标
	 * @param gogal
	 */
	public void save(Gogal g) {
		String sql = "insert into t_gogal(id,name,`desc`,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		db.executeSQL(sql, g.getId(),g.getName(),g.getDesc(),g.getCreatetime(),g.getEmployeeId(),g.getProjectId());
	}
	/**
	 * 更新目标
	 * @param gogal
	 */
	public void update(Gogal g) {
		String sql = "update t_gogal set name=?,`desc`=? where id =?";
		db.executeSQL(sql, g.getName(),g.getDesc(),g.getId());
	}
	/**
	 * 删除目标
	 * @param gogalId
	 */
	public void delGogalByGogalId(String gogalId) {
		String sql = "delete from t_gogal where id = ?";
		db.executeSQL(sql, gogalId);
	}
}
